package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectTypeInfoEnum;
import com.jingdianjichi.subject.domain.convert.JudgeSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 *  判断题目的策略类
 *
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectJudgeService subjectJudgeService;
    /**
     * 枚举身份的识别
     */
    @Override
    public SubjectTypeInfoEnum getHandlerType() {
        return SubjectTypeInfoEnum.JUDGE;
    }

    /**
     *判断题目的插入
     *
     * @param subjectInfoBo
     */
    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        SubjectJudge subjectJudge=new SubjectJudge();
        SubjectAnswerBo subjectAnswerBo=subjectInfoBo.getOptionList().get(0);
        subjectJudge.setSubjectId(subjectInfoBo.getId());
        subjectJudge.setIsCorrect(subjectAnswerBo.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
       subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectJudge subjectJudge=new SubjectJudge();
        subjectJudge.setSubjectId(Long.valueOf(subjectId));
       List<SubjectJudge> result=subjectJudgeService.queryByCondition(subjectJudge);
       List<SubjectAnswerBo> subjectAnswerBoList= JudgeSubjectConverter.INSTANCE.convertEntitytoBoList(result);
       SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
       subjectOptionBO.setOptionList(subjectAnswerBoList);
        return subjectOptionBO;
    }
}
